package service

import (
	"code.huawei.com/tdc-sdk/mgmt"
	"nft-server/common"
	"nft-server/dao"
	"nft-server/model"
)

type MetaDataDetail struct {
	AssetUrl    string                   `json:"url"`
	Description string                   `json:"description"`
	Category    int                      `json:"category"`
	Name        string                   `json:"name"`
	Properties  map[string]mgmt.Property `json:"properties"`
}

//MetaData中设置的Properties成员
const (
	PropertiesOfIssuer = "issuer"
)

//BuildMetaDataPropertiesForMint根据实际情况构建业务属性
func BuildMetaDataPropertiesForMint(issuer string) map[string]string {
	property := make(map[string]string)
	property[PropertiesOfIssuer] = issuer
	return property
}

//从MetaDataDetail中取出相关属性，且必须为MetaData中设置的Properties成员
func GetValueFromMetaDataProperties(metadata *MetaDataDetail) map[string]string {
	properties := make(map[string]string)
	for propertyKey, propertyValue := range metadata.Properties {
		if propertyKey == PropertiesOfIssuer {
			properties[propertyKey] = propertyValue.Value
		}
	}
	return properties
}

func GetMetaDataWithCollections(userId int, collectionIndex []string) (metadata []*model.MetaData, dataSize int, err error) {
	return dao.GetMetaDataDao().GetMetaDataByCollections(userId, collectionIndex)
}

func GetMetaDataWithSubCollectionIndex(userId int, collectionIndex string, subCollectionIndex []int) ([]*model.MetaData, int, error) {
	return dao.GetMetaDataDao().GetMetaDataExact(userId, collectionIndex, subCollectionIndex)
}
func GetMetaDataByIdentity(identity common.CollectionIdentity) (*model.MetaData, error) {
	return dao.GetMetaDataDao().GetMetaDataByIdentity(identity.CollectionIndex, identity.SubCollectionIndex)
}

func SaveMetaData(userId int, collectionIndex string, subCollectionIndex int, tokenURI *mgmt.BatchMintData) error {
	tokenString, _ := tokenURI.TokenURI.ToString()
	metadata := &model.MetaData{
		UserId:             userId,
		CollectionIndex:    collectionIndex,
		SubCollectionIndex: subCollectionIndex,
		StartTokenID:       tokenURI.StartTokenID,
		Amount:             tokenURI.Amount,
		TokenURI:           tokenString,
	}
	return dao.GetMetaDataDao().SaveMetaDataInfo(metadata)
}

func GetMetaDataWithCollectionIndexAndTokenIdWithoutUserId(collectionIndex, tokenId string) (metadata *model.MetaData, err error) {
	return dao.GetMetaDataDao().GetMetaDataByCollectionIndexAndTokenId(collectionIndex, tokenId)
}
